#pragma once

#include "common/common.h"
#include "string/stringmanipulations.h"

#if defined(WIN32) || defined(WINDOWS)

#else
char * _compat_strerror(char* messageBuffer, int_r bufferSize, ErrorCode);
#endif

ErrorCode  PILOGetLastSystemErrorNumber();
const char*     PILOGetLastSystemErrorMessage(char*    messageBuffer, int_r bufferSize, ErrorCode number);
const wchar_t*  PILOGetLastSystemErrorMessage(wchar_t* messageBuffer, int_r bufferSize, ErrorCode number);
TriState      PILOGetLastSystemError(char*    messageBuffer, int_r bufferSize);
TriState      PILOGetLastSystemError(wchar_t* messageBuffer, int_r bufferSize);

template <typename T>
TriState PILOTGetLastSystemError(T & string)
{
    ErrorCode err = PILOGetLastSystemErrorNumber();
    if (MC_SUCCESS == err) {
        return EnumTriStateNo;
    }

    PILOGetLastSystemErrorMessage(string.data(), string.capacity(), err);
    string.updateSize();
    return EnumTriStateYes;
}

template <typename T>
void PILOTGetLastSystemError(T & string, ErrorCode errorNumber)
{
    PILOGetLastSystemErrorMessage(string.data(),  string.capacity(), errorNumber);
    string.updateSize();
}

